<?php

namespace Apps\AppApi\Method\Activity;
use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Libs\PicUrl;
use Apps\Common\Models\Activity;
use Apps\Common\Models\BeautyParlor;
use Apps\Common\Models\OmBanner;
use Apps\Common\Models\SysOpenCity;
use PhalconPlus\Api\Common\ResultStatus;


/**
 * 活动列表，可通过分类筛选
 * @package Apps\AppApi\Method\Activity
 */
class GetList extends BaseMethod
{
    /**
     * @var \Apps\Common\Models\Activity array
     */
    private $activity = [];

    public function run()
    {
        $city_code = $this->getDataItem('city_code',0);
        $page = intval($this->getDataItem('page',1));
        $state = $this->getDataItem('state', 1);

        $limit = $this->getConfig()->limit;
        $offset = ($page-1)*$limit;

        $city = $this->getCity($city_code);

        if(!$city || $city['city_id'] <= 0) {
            $where = "activity_state = 1 and city_id = 0";
//            return $this->errorLog(ResultStatus::CITY_NOT_OPEN, '城市未开通');
//            $cityId = 2;
        } else {
//            $cityId = $city['city_id'];
            $where = "activity_state = 1 and city_id in({$city['city_id']}, 0)";
        }
        $date = date('Y-m-d H:i:s');

        if ($state == 1) { // 进行中
            $where .= " and activity_start_time <= '{$date}' and activity_end_time >= '{$date}'";
            $order = 'activity_end_time';
        } else { // 即将开始
            $where .= " and activity_start_time >= '{$date}'";
            $order = 'activity_start_time';
        }

        $this->activity = Activity::query()
            ->columns([
                'IF(city_id = 0, 1, 0) as city',
                'city_id',
                'activity_id id',
                'activity_type type',
                'activity_title as title',
                'activity_cover as cover',
                'activity_start_time',
                'activity_end_time',
                'activity_intro',
                'out_link',
                'views',
                'comments'
//            ])->where($where)->orderBy("$order")->limit($limit, $offset)->execute()->toArray();
            ])->where($where)->orderBy("city DESC, {$order}")->limit($limit, $offset)->execute()->toArray();

        foreach ($this->activity as $k => $val) {
            if ($val['city_id'] == 0) {
                $this->activity[$k]['city_name'] = '全国';
            } else {
                $this->activity[$k]['city_name'] = $city['city_name'] ?: '';
            }
            $this->activity[$k]['cover'] = PicUrl::ActivityCover($this->activity[$k]['cover'], $this->getDi());
            unset($this->activity[$k]['city_id']);
        }

        $this->setResult($this->activity);
    }

    /*private function getRecommend($cityId)
    {
        $bannerBp = OmBanner::query()
            ->columns('banner_data')
            ->where('city_id = :cid: AND banner_name = :key:', ['cid' => $cityId, 'key' => 'index_bp'])
            ->execute()
            ->getFirst();
        if($bannerBp) {
            $data = BeautyParlor::query()
                ->columns([
                    'bp_id as id',
                    'bp_name as name',
                    'bp_cover as cover',
                    'bp_address as address',
                    'bp_medal as medal',
                ])
                ->where('bp_state>0 and bp_id=:id:')
                ->bind(['id' => $bannerBp->banner_data])
                ->execute()
                ->getFirst();
            $bpData = [
                "id" => $data->id,
                "name" => $data->name,
                "cover" => PicUrl::BeautyParlorCover($data->cover, $this->getDi()),
                "address" => $data->address,
                "tags" => $this->getBeautyParlorTags($data->id),
                "medal_refund" => \Apps\Common\Libs\BeautyParlor::getMedalRefund($data->medal),
                "medal_verify" => \Apps\Common\Libs\BeautyParlor::getMedalVerify($data->medal),
            ];
        } else {
            $bpData = null;
        }
        $pics = OmBanner::query()
            ->columns('banner_data')
            ->where('city_id = :cid: AND banner_name = :key:', ['cid' => $cityId, 'key' => 'index_pics'])
            ->execute()
            ->getFirst();
        if($pics) {
            $picsData = json_decode($pics->banner_data, true);
        } else {
            $picsData = [];
        }
        for($i = 0; $i < count($picsData); $i++) {
            $picsData[$i]['url'] = PicUrl::DefaultPics($picsData[$i]['url'], $this->getDi());
        }
        return [
            'pic' => $picsData,
            'beauty_parlor' => $bpData
        ];
    }*/

}